<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>优田儿童成长中心</title>
    <link href="/static/assets/fontAwesome/css/fontawesome-all.min.css" rel="stylesheet">
    <link href="/static/assets/css/lib/themify-icons.css" rel="stylesheet">
    <link href="/static/assets/css/lib/mmc-chat.css" rel="stylesheet"/>
    <link href="/static/assets/css/lib/sidebar.css" rel="stylesheet">
    <link href="/static/assets/css/lib/bootstrap.min.css" rel="stylesheet">
    <link href="/static/assets/css/lib/nixon.css" rel="stylesheet">
    <link href="/static/assets/lib/lobipanel/css/lobipanel.min.css" rel="stylesheet">
    <link href="/static/assets/css/style.css" rel="stylesheet">
</head>
<body>
<div class="sidebar sidebar-hide-to-small sidebar-shrink sidebar-gestures">
    <div class="nano">
        <div class="nano-content">
            <ul>
                <li>
                    <a href="/home/" id="stats-btn"><i class="ti-bar-chart"></i> 统计数据</a>
                </li>
                <li class="active">
                    <a href="/teacher/"><i class="ti-user"></i> 教师管理<span
                            class="sidebar-collapse-icon ti-angle-right"></span></a>
                </li>
                <li>
                    <a href="/child/"><i class="ti-user"></i> 幼儿管理</a>
                </li>
                <li>
                    <a href="/posts/"><i class="ti-desktop"></i> 岗位管理</a>
                </li>
            </ul>
        </div>
    </div>
</div>
<div class="header">
    <div class="pull-left">
        <div class="logo">
            <a>
                <img id="logoImg" src="/static/img/yt1.png" data-logo_big="/static/img/yt1.png"
                     data-logo_small="/static/img/yt3.png" alt="Nixon"/>
            </a>
        </div>
        <div class="hamburger sidebar-toggle">
            <span class="ti-menu"></span>
        </div>
    </div>
    <div class="pull-right p-r-15">
        <ul>
            <li class="header-icon dib">
                <img class="avatar-img" src="/static/assets/images/avatar/1.jpg" alt=""/> <span
                    class="user-avatar"><span th:text="${session.username}"></span>&nbsp;&nbsp;&nbsp;<i
                    class="ti-angle-down f-s-10"></i></span>
                <div class="drop-down dropdown-profile">
                    <div class="dropdown-content-body">
                        <ul>
                            <li><a href="/exit/"><i class="ti-power-off"></i> <span>注销</span></a></li>
                        </ul>
                    </div>
                </div>
            </li>
        </ul>
    </div>
</div>
<div class="content-wrap">
    <div class="main">
        <div class="container-fluid">
            <div class="row">
                <div class="col-lg-8 p-0">
                    <div class="page-header">
                        <div class="page-title">
                            <h1>教师管理</h1>
                        </div>
                    </div>
                </div>
                <div class="col-lg-4 p-0">
                    <div class="page-header">
                        <div class="page-title">
                            <ol class="breadcrumb text-right">
                                <li><a href="/teacher/">教师信息</a></li>
                                <li class="active">教师管理</li>
                            </ol>
                        </div>
                    </div>
                </div>
            </div>
            <div class="main-content">
                <div class="row">
                    <div class="card alert">
                        <div class="card-header">
                            <h4>教师信息</h4>
                            <div class="card-header-right-icon">
                                <a href="/add_teacher/" class="btn btn-success m-b-10 m-l-5">添加教师</a>
                            </div>
                        </div>
                        <div class="card-body">
                            <table class="table table-responsive table-hover ">
                                <thead>
                                <tr>
                                    <th>编号</th>
                                    <th>姓名</th>
                                    <th>性别</th>
                                    <th>类型</th>
                                    <th>照片</th>
                                    <th>年龄</th>
                                    <th>毕业学校</th>
                                    <th>联系方式</th>
                                    <th>任职状态</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody id="teacher-table-body">
                                </tbody>
                            </table>
                            <nav>
                                <ul class="pagination" id="pagination">
                                </ul>
                            </nav>
                        </div>
                    </div>
                </div>
            </div>
            Copyright &copy; 2024
        </div>
    </div>
</div>
<script src="/static/assets/js/lib/jquery.min.js"></script>
<script src="/static/assets/js/lib/jquery.nanoscroller.min.js"></script>
<script src="/static/assets/js/lib/sidebar.js"></script>
<script src="/static/assets/js/lib/bootstrap.min.js"></script>
<script src="/static/assets/js/lib/mmc-common.js"></script>
<script src="/static/assets/js/lib/mmc-chat.js"></script>
<script src="/static/assets/js/lib/chart-js/Chart.bundle.js"></script>
<script src="/static/assets/js/lib/chart-js/chartjs-init.js"></script>
<script src="/static/assets/js/lib/datamap/d3.min.js"></script>
<script src="/static/assets/js/lib/datamap/topojson.js"></script>
<script src="/static/assets/js/lib/datamap/datamaps.world.min.js"></script>
<script src="/static/assets/js/lib/datamap/datamap-init.js"></script>
<script src="/static/assets/lib/lobipanel/js/lobipanel.js"></script>
<script src="/static/assets/js/scripts.js"></script>
<script>
    $(document).ready(function () {
        loadTeacherData(1, 6);
    });

    function loadTeacherData(page = 1, size = 6) {
        $.ajax({
            url: "/api/v1/manager/teacher/list",
            method: "GET",
            data: {page: page, size: size},
            success: function (res) {
                if (res.code === 200) {
                    const pageData = res.data;
                    const list = pageData.list;
                    const total = pageData.total;
                    const currentPage = pageData.pageNum;
                    const pageSize = pageData.pageSize;
                    renderTable(list);
                    renderPagination(total, currentPage, pageSize);
                } else {
                    alert("加载失败：" + res.msg);
                }
            },
            error: function (xhr, status, error) {
                console.error("AJAX请求失败:", error);
                alert("加载失败，请检查网络连接");
            }
        });
    }

    function renderTable(list) {
        const tbody = $("#teacher-table-body");
        tbody.empty();

        list.forEach(t => {
            let typeStr = t.ttype === 1 ? '<span class="badge badge-primary">托育老师</span>' : '<span class="badge badge-danger">托管老师</span>';
            let img = t.timg ? `<img src="data:image/jpeg;base64,${t.timg}" width="50" height="50" style="object-fit: cover; border-radius: 5px;">` : `<img src="/static/img/default.png" width="50" height="50" style="object-fit: cover; border-radius: 5px;">`;
            let statusStr = t.flag === '1' ? '<span class="badge badge-success">在职</span>' : '<span class="badge badge-default" style="background-color: #8a8a8a">离职</span>';

            let actions = `
            ${t.flag === '1' ? `<a href="/update_teacher/?tid=${t.tid}" class="btn btn-success">编辑</a>
            <a href="javascript:;" onclick="del(${t.tid})" class="btn btn-danger">离职</a>` : ''}
            <a href="/mess/?tid=${t.tid}" class="btn btn-info">详情</a> |
            <a href="/teacher_sign/?tid=${t.tid}" class="btn btn-primary">签到记录</a>
            <a href="/teacher_sal/list?id=${t.tid}&ttype=${t.ttype}" class="btn btn-warning">工资管理</a>
        `;

            const row = `
            <tr>
                <th scope="row">${t.tid}</th>
                <td>${t.tname}</td>
                <td>${t.tsex}</td>
                <td>${typeStr}</td>
                <td>${img}</td>
                <td>${t.tage}</td>
                <td>${t.tschool}</td>
                <td>${t.tphone}</td>
                <td>${statusStr}</td>
                <td>${actions}</td>
            </tr>
        `;
            tbody.append(row);
        });
    }

    function renderPagination(total, currentPage, pageSize) {
        const totalPages = Math.ceil(total / pageSize);
        const pagination = $("#pagination");
        pagination.empty();

        if (currentPage > 1) {
            pagination.append(`<li class="page-item"><a class="page-link" href="javascript:;" data-page="${currentPage - 1}">上一页</a></li>`);
        } else {
            pagination.append(`<li class="page-item disabled"><a class="page-link">上一页</a></li>`);
        }

        const startPage = Math.max(1, currentPage - 2);
        const endPage = Math.min(totalPages, currentPage + 2);

        if (startPage > 1) {
            pagination.append(`<li class="page-item"><a class="page-link" href="javascript:;" data-page="1">1</a></li>`);
            if (startPage > 2) {
                pagination.append(`<li class="page-item disabled"><a class="page-link">...</a></li>`);
            }
        }

        for (let i = startPage; i <= endPage; i++) {
            let active = i === currentPage ? "active" : "";
            pagination.append(`<li class="page-item ${active}"><a class="page-link" href="javascript:;" data-page="${i}">${i}</a></li>`);
        }

        if (endPage < totalPages) {
            if (endPage < totalPages - 1) {
                pagination.append(`<li class="page-item disabled"><a class="page-link">...</a></li>`);
            }
            pagination.append(`<li class="page-item"><a class="page-link" href="javascript:;" data-page="${totalPages}">${totalPages}</a></li>`);
        }

        if (currentPage < totalPages) {
            pagination.append(`<li class="page-item"><a class="page-link" href="javascript:;" data-page="${currentPage + 1}">下一页</a></li>`);
        } else {
            pagination.append(`<li class="page-item disabled"><a class="page-link">下一页</a></li>`);
        }

        $(".pagination .page-link").off("click").on("click", function () {
            const page = parseInt($(this).data("page"));
            if (!isNaN(page)) {
                loadTeacherData(page, pageSize);
            }
        });
    }

    function del(id) {
        if (confirm("确认让该教师离职？")) {
            $.post("/api/v1/manager/teacher/leave", {tid: id}, function (res) {
                if (res.code === 200) {
                    alert("教师已成功离职！");
                    loadTeacherData();
                } else {
                    alert("离职失败：" + res.msg);
                }
            });
        }
    }
</script>
<style>
    .copyrights {
        text-indent: -9999px;
        height: 0;
        line-height: 0;
        font-size: 0;
        overflow: hidden;
    }
</style>
<div class="copyrights">
    <h3>&copy; 版权所有 | BYHK工作室2024</h3>
</div>
</body>

</html>